package com.testmanager.testmananger.quantitative.service;

import com.testmanager.testmananger.quantitative.domain.WorkerEntity;
import com.testmanager.testmananger.quantitative.dto.WorkerDto;
import com.testmanager.testmananger.quantitative.mapper.WorkerMapper;
import com.testmanager.testmananger.quantitative.repository.WorkerRepository;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.message.LoggerNameAwareMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
@Slf4j
public class WorkService {

    @Autowired
    WorkerRepository workerRepository;



     public void saveWorkerDtos(List<WorkerDto> workerDtos,long ownNo){
         if (workerDtos != null ){
             List<WorkerEntity> collect = workerDtos.stream()
                     .filter(Objects::nonNull)
                     .map(t -> WorkerMapper.toWorkerEntity(ownNo, t))
                     .collect(Collectors.toList());
             saveWorkers(collect, ownNo);
         }
     }




    public void saveWorkers(List<WorkerEntity> workerEntities,long ownNo){
    if ( null == workerEntities    || workerEntities.isEmpty()) {
     //   int i =workerRepository.findWorkerEntitiesByOwnerNo(ownNo).size();
       if(workerRepository.findWorkerEntitiesByOwnerNo(ownNo).size() != 0) {
           workerRepository.deleteByOwnerNo(ownNo);
       }
        return;
    }
    List<WorkerEntity> workerEntitiesByOwnerNo = workerRepository.findWorkerEntitiesByOwnerNo(ownNo);
    for(WorkerEntity i : workerEntities){
        if (!existInList(i, workerEntitiesByOwnerNo)){ //传入的worker不存在于数据库时
            workerRepository.save(i);
        }
    }

    for (WorkerEntity i : workerEntitiesByOwnerNo){
        if (!existInList(i, workerEntities)){//worker不存在传入的参数中时，删除数据库中的对应值
            try {
                workerRepository.deleteById(i.getId());
            }catch (Exception e){

            }

        }
    }




    }

    public void saveWorker(WorkerEntity workerEntity){
        if (workerEntity == null){
            return;
        }
        long ownerNo = workerEntity.getOwnerNo();
        //List<WorkerEntity> workerEntities = workerRepository.findWorkerEntitiesByOwnerNo(ownerNo);
        WorkerEntity workerEntityByOwnerNo = workerRepository.findWorkerEntityByOwnerNo(ownerNo);
        if (null != workerEntityByOwnerNo){
            workerEntityByOwnerNo.setUserId(workerEntity.getUserId());
            workerEntityByOwnerNo.setUserName(workerEntity.getUserName());
            workerRepository.save(workerEntityByOwnerNo);
        }else{
            workerRepository.save(workerEntity);
        }


    }

    private boolean existInList(WorkerEntity t,List<WorkerEntity> workerEntities){
        for (WorkerEntity  y: workerEntities){
            if (t.getUserName().trim().equals(y.getUserName().trim())){
                return true; //存在
            }
        }
        return false;
    }


}
